#include <stdio.h>
#include <cstdlib>
#include <stack>
#ifndef _Stack_h

struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode Stack;
typedef int ElementType;

int IsEmpty( Stack S);
Stack CreatStack( void );
void DisposeStack( Stack S);
void MakeEmpty( Stack S );
void Push( ElementType X,Stack S );
ElementType Top( Stack S );
void Pop( Stack S );

#endif /*_Stack_h */

struct Node
{
    int Element;
    PtrToNode  Next;
};

Stack
CreatStack( void )
{
    Stack S;

    S = malloc( sizeof ( struct Node));
    if(S == NULL)
        printf/*FatalError*/("Out of space!!!");
    S -> Next = NULL;
    MakeEmpty( S );
    return S;
}

void MakeEmpty( Stack S)
{
    if( S == NULL)
        printf/*Error*/("Must use CreatStack first");
    else
        while(!IsEmpty( S ))
            Pop( S );
}
void
Push( ElementType X, Stack S)
{
    PtrToNode TmpCell;

    TmpCell = malloc( sizeof ( struct Node));
    if( TmpCell == NULL )
        printf/*FatalError*/( "Out of space!!!");
    else
    {
        TmpCell -> Element = X;
        TmpCell -> Next = S -> Next;
        S -> Next = TmpCell;
    }
}

int
Top (Stack S)
{
    if( !IsEmpty( S ))
        return S -> Next ->Element;
    printf/*Error*/("Empty stack");
    return 0;
}
void
Pop (Stack S)
{
    PtrToNode FirstCell;

    if(IsEmpty( S ) )
        printf/*Error*/("Empty stack");
    else
    {
        FirstCell = S ->Next;
        S ->Next = S ->Next->Next;
        free( FirstCell );
    }
}